package com.zoke.daycarpool.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.zoke.daycarpool.R;
import com.zoke.daycarpool.base.BaseAppFragment;
import com.zoke.daycarpool.config.ImageConfig;
import com.zoke.daycarpool.config.CarpoolConfig.MessageCode;
import com.zoke.daycarpool.domain.UserBeen;
import com.zoke.daycarpool.domain.UserInstense;
import com.zoke.daycarpool.ui.AboutUs;
import com.zoke.daycarpool.ui.FeedBack;
import com.zoke.daycarpool.ui.MyPublish;
import com.zoke.daycarpool.ui.PersonEdit;
import com.zoke.support.view.ZokeToast;

/**
 * 我是车主/我是乘客布局
 * 
 * @author jack
 * 
 */
public class MineFragment extends BaseAppFragment {
	private RelativeLayout mPersonRl;
	private RelativeLayout mReviewsForme;
	private RelativeLayout mMyPublish;
	private RelativeLayout mAboutUsRl;
	private RelativeLayout mVersionUpdateRl;
	private TextView mNameTv;

	private UserBeen mUser;
	private ImageView mHeadIv;
	private ImageView mSexIv;

	@Override
	protected void onCreateView(Bundle savedInstanceState) {
		super.onCreateView(savedInstanceState);
		setContentView(R.layout.fragment_mine);
		mUser = UserInstense.getInstense(getActivity()).getUser();
		findView();
		doEvent();
	}

	@Override
	protected void handleMsg(Message msg) {
		super.handleMsg(msg);
		switch (msg.what) {
		case MessageCode.UPDATE_MIENFRAGMENT:
			mUser = UserInstense.getInstense(getActivity()).getUser();
			setUserData(mUser);
			break;

		default:
			break;
		}
	}

	@Override
	protected void findView() {
		mPersonRl = (RelativeLayout) findViewById(R.id.mine_person_rl);
		// 意见反馈
		mReviewsForme = (RelativeLayout) findViewById(R.id.reviews_forme);
		mMyPublish = (RelativeLayout) findViewById(R.id.editpersonmsg_rl);
		mAboutUsRl = (RelativeLayout) findViewById(R.id.mine_aboutus);
		mVersionUpdateRl = (RelativeLayout) findViewById(R.id.mine_setting_rl);
		mNameTv = (TextView) findViewById(R.id._carpool_mine_name_tv);
		mHeadIv = (ImageView) findViewById(R.id._carpool_mine_head_iv);
		mSexIv = (ImageView) findViewById(R.id._carpool_mine_sex_iv);
		setUserData(mUser);
	}

	/** 设置User数据到界面 **/
	private void setUserData(UserBeen userBeen) {
		mNameTv.setText(mUser.getUserName());
		ImageLoader.getInstance().displayImage(mUser.getPhoto(), mHeadIv,
				ImageConfig.getHeadOptions());
		mtiManager.setTitle(userBeen.getIdentify().equals("1") ? "我是乘客"
				: "我是车主");
		mSexIv.setBackgroundResource(userBeen.getSex().equals("1") ? R.drawable.man
				: R.drawable.woman);
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("MineScreen"); // 统计页面
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("MineScreen");
	}

	@Override
	protected void doEvent() {
		mPersonRl.setOnClickListener(this);
		mReviewsForme.setOnClickListener(this);
		mMyPublish.setOnClickListener(this);
		mAboutUsRl.setOnClickListener(this);
		mVersionUpdateRl.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
		case R.id.mine_person_rl:
			mIntent = new Intent(getActivity(), PersonEdit.class);
			startUI(mIntent);
			break;
		case R.id.reviews_forme:
			mIntent = new Intent(getActivity(), FeedBack.class);
			startUI(mIntent);
			break;
		case R.id.editpersonmsg_rl:
			mIntent = new Intent(getActivity(), MyPublish.class);
			startUI(mIntent);
			break;
		case R.id.mine_aboutus:
			mIntent = new Intent(getActivity(), AboutUs.class);
			startUI(mIntent);
			break;
		case R.id.mine_setting_rl:
			ZokeToast.show(getActivity(), "正在检测版本……");
			UmengUpdateAgent.forceUpdate(getActivity());
			break;
		default:
			break;
		}
	}
}
